#include "odeCube.h"
#include <GL/glut.h>


cOdeCube::cOdeCube(dWorldID world,dSpaceID space,dReal width,dReal height,dReal depth,dReal mass)
{
	body = dBodyCreate(world);
	geom = dCreateBox(space,width,height,depth);

	dMass m;
	dMassSetBox(&m,1.0f,width,height,depth);   
	dMassAdjust(&m,mass);  
	dBodySetMass(body,&m);
	dGeomSetBody(geom,body); 

	// textures
	m_pTexBox1 = CTextureManager::GetSingletonPtr()->FindTexture("textures/box1.png");
	m_pTexBox2 = CTextureManager::GetSingletonPtr()->FindTexture("textures/box2.png");
	m_pTexBox3 = CTextureManager::GetSingletonPtr()->FindTexture("textures/box3.png");
	m_pTexBox4 = CTextureManager::GetSingletonPtr()->FindTexture("textures/box4.png");
	m_pTexBox5 = CTextureManager::GetSingletonPtr()->FindTexture("textures/box5.png");
}

void cOdeCube::renderInLocalFrame() const
{
	dVector3 lengths;
	dGeomBoxGetLengths(geom,lengths);
	glScalef(lengths[0],lengths[1],lengths[2]);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,color);
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	glDisable(GL_LIGHT0);

	m_pTexBox1->BindTexture();
	glColor4f(1.0f, 1.0f, 1.0f, 0.9f);

	glBegin(GL_QUADS);
		glNormal3f(0.0,0.0,1.0);

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-0.5, 0.5, 0.5);
	
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f( 0.5, 0.5, 0.5);

		glTexCoord2f(0.0f, 0.0f);
		glVertex3f( 0.5, -0.5, 0.5);

		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-0.5, -0.5, 0.5);
	glEnd();
		
	m_pTexBox2->BindTexture();
	glBegin(GL_QUADS);
		glNormal3f(0.0,0.0,-1.0);

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( 0.5, 0.5, -0.5);
		
		glTexCoord2f(0.0, 1.0);
		glVertex3f(-0.5, 0.5, -0.5);
		
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-0.5, -0.5, -0.5);
		
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f( 0.5, -0.5, -0.5);
	glEnd();


	m_pTexBox3->BindTexture();
	glBegin(GL_QUADS);
		glNormal3f(0.0,1.0,0.0);

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-0.5, 0.5, -0.5);
		
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f( 0.5, 0.5, -0.5);
		
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f( 0.5, 0.5, 0.5);
		
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-0.5, 0.5, 0.5);
	glEnd();
		

	m_pTexBox3->BindTexture();
	glBegin(GL_QUADS);
		glNormal3f(0.0,-1.0,0.0);

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-0.5, -0.5, 0.5);

		glTexCoord2f(0.0f, 1.0f);
		glVertex3f( 0.5, -0.5, 0.5);

		glTexCoord2f(0.0f, 0.0f);
		glVertex3f( 0.5, -0.5, -0.5);

		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-0.5, -0.5, -0.5);
	glEnd();

	m_pTexBox4->BindTexture();
	glBegin(GL_QUADS);
		glNormal3f(-1.0,0.0,0.0);

		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-0.5, 0.5, -0.5);

		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-0.5, 0.5,  0.5);

		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-0.5, -0.5, 0.5);

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-0.5, -0.5, -0.5);
	glEnd();

	m_pTexBox5->BindTexture();
	glBegin(GL_QUADS);
		glNormal3f(1.0,0.0,0.0);

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( 0.5, 0.5,  0.5);

		glTexCoord2f(0.0f, 1.0f);
		glVertex3f( 0.5, 0.5,  -0.5);

		glTexCoord2f(0.0f, 0.0f);
		glVertex3f( 0.5, -0.5, -0.5);

		glTexCoord2f(1.0f, 0.0f);
		glVertex3f( 0.5, -0.5,  0.5);
	glEnd();
}